importgreenfoot.*;// (World, Actor, GreenfootImage, and Greenfoot)/**
* An ant that collects food.
*
* @author Michael Kolling
* @version 1.1
*/publicclass Ant extends Creature
{/** Every how many steps can we place a pheromone drop. */privatestaticfinalint MAX_PH_LEVEL =18;/** How long do we keep direction after finding pheromones. */privatestaticfinalint PH_TIME =30;/** Indicate whether we have any food with us. */privateboolean carryingFood =false;/** How much pheromone do we have right now. */privateint pheromoneLevel = MAX_PH_LEVEL;/** How well do we remember the last pheromone - larger number: more recent */privateint foundLastPheromone =0;/**
* Create an ant with a given home hill. The initial speed is zero (not moving).
*/public Ant(AntHill home){
setHomeHill(home);}/**
* Do what an ant's gotta do.
*/publicvoid act(){if(carryingFood){
walkTowardsHome();
handlePheromoneDrop();
checkHome();}else{
searchForFood();}}/**
* Walk around in search of food.
*/privatevoid searchForFood(){if(foundLastPheromone >0){// if we can still remember...
foundLastPheromone--;
walkAwayFromHome();}elseif(smellPheromone()){
walkTowardsPheromone();}else{
randomWalk();}
checkFood();}/**
* Are we home? Drop the food if we are, and start heading back out.
*/privatevoid checkHome(){if(atHome()){
dropFood();}}/**
* Are we home?
*/privateboolean atHome(){if(getHomeHill()!=null){return(Math.abs(getX()- getHomeHill().getX())<4)&&(Math.abs(getY()- getHomeHill().getY())<4);}else{returnfalse;}}/**
* Is there any food here where we are? If so, take some!.
*/publicvoid checkFood(){
Food food =(Food) getOneIntersectingObject(Food.class);if(food !=null){
takeFood(food);}}/**
* Take some food from a fool pile.
*/privatevoid takeFood(Food food){
carryingFood =true;
food.takeSome();
setImage("ant-with-food.gif");}/**
* Drop our food in the ant hill.
*/privatevoid dropFood(){
carryingFood =false;
getHomeHill().countFood();
setImage("ant.gif");}/**
* Check whether we can drop some pheromone yet. If we can, do it.
*/privatevoid handlePheromoneDrop(){if(pheromoneLevel == MAX_PH_LEVEL){
Pheromone ph =new Pheromone();
getWorld().addObject(ph, getX(), getY());
pheromoneLevel =0;}else{
pheromoneLevel++;}}/**
* Check whether we can smell pheromones. If we can, return true, otherwise return false.
*/publicboolean smellPheromone(){
Actor ph = getOneIntersectingObject(Pheromone.class);return(ph !=null);}/**
* If we can smell some pheromone, walk towards it. If not, do nothing.
*/publicvoid walkTowardsPheromone(){
Actor ph = getOneIntersectingObject(Pheromone.class);if(ph !=null){
headTowards(ph);
walk();if(ph.getX()== getX()&& ph.getY()== getY()){
foundLastPheromone = PH_TIME;}}}}
返回